// pages/orders/orders.js
const app = getApp();
import {
  OrdersModel
} from "../../utils/models/orders/Orders-models";
var ordersModel = new OrdersModel;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    list: [],
    timeList:[],
  },

  // 点击tab选项卡
  changeTab: function(e) {
    for(var i = 0;i<=1000;i++){    //暴力清除定时器
      clearInterval(i);
    }
    var index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index
    },function() {
      wx.showLoading({
        title: '正在加载中...',
      })
      setTimeout(function() {
        wx.hideLoading()
      },500)
    });
    this.getOrderList(this.data.activeIndex);
  },

  getOrderList:function(type){
    
    let param = {'status':type};
    let that = this;
    ordersModel.getOrderList(param,function(res){
      if(res.code > 0){
        var data = res.data;
        that.setData({
          'list': data
        });
        for(var i = 0;i<data.length;i++){
          data[i].timecli = '';
          if(data[i].status == 0){
            var nowTime = Date.parse(new Date());
            var difTime = (data[i].pay_expire)  - (nowTime/1000);
            var timecli = that.formatSeconds(difTime);
            data[i].timecli = timecli;
            that.setData({
              'list': data
            });
            var idx = i;
            that.timeclis(difTime,idx,data,'timer'+i);
          }
        }
      }
    });
  },
  timeclis: function (difTime, i, data,timer){
    if (difTime > 0 && data.length > 0 ){
      var that = this;
      var timeList = {};
      timer = setInterval(function(){
        var timecli = that.formatSeconds(difTime);
        data[i].timecli = timecli;
        if (difTime > 0) {
          difTime--;
          that.setData({
            list: data
          })
        }
      },1000)
      timeList['clear'+i] = timer;
      that.setData({
        'timeList':timeList
      })
    }
  },
  //将秒数转换为时分秒格式
  formatSeconds:function (value) {
    var theTime = parseInt(value);// 秒
    var middle = 0;// 分
    var hour = 0;// 小时

    if(theTime > 60) {
      middle = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
      if (middle > 60) {
        hour = parseInt(middle / 60);
        middle = parseInt(middle % 60);
      }
    }
    var result = "" + parseInt(theTime);
    if (middle > 0) {
      result = "" + parseInt(middle) + ":" + result;
    }
    if (hour > 0) {
      result = "" + parseInt(hour) + ":" + result;
    }else{
      result = "00" + ":" + result;
    }
    return result;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.index) {
      this.setData({
        activeIndex: options.index
      })
    }
    this.getOrderList(options.index);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getOrderList(this.data.activeIndex);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})